<script lang="ts">
    import { effect, reactive } from "@feng3d/reactivity";
    import { onMount } from "svelte";
    import { app, Todo } from "./App";
    import Counter from "./components/Counter.svelte";
    import TodoList from "./components/TodoList.svelte";
    import ToggleButton from "./components/ToggleButton.svelte";
    import UserCard from "./components/UserCard.svelte";

    let currentTime: string = "";
    let name: string = "";
    let isDarkMode: boolean = false;
    let count: number = 0;
    let todos: Todo[] = [];
    let completedTodos: number = 0;
    let totalTodos: number = 0;
    let progress: number = 0;

    effect(() => {
        const r_app = reactive(app);

        currentTime = r_app.currentTime;
        name = r_app.name;
        isDarkMode = r_app.isDarkMode;
        count = r_app.count;
        todos = r_app.todos.map((todo) => ({ ...todo }));
        completedTodos = r_app.completedTodos;
        totalTodos = r_app.totalTodos;
        progress = r_app.progress;
    });

    function onNameInput(e: Event): void {
        const input = e.target as HTMLInputElement;
        const r_app = reactive(app);
        r_app.name = input.value;
    }

    // 生命周期
    onMount(() => {
        return app.init();
    });
</script>

<svelte:head>
    <title>Svelte 示例项目</title>
    <style>
        :global(body) {
            margin: 0;
            font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Roboto",
                sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            transition: all 0.3s ease;
            color: #333;
        }

        :global(body.dark-mode) {
            background: linear-gradient(135deg, #2c3e50 0%, #34495e 100%);
            color: #ecf0f1;
        }

        :global(*) {
            box-sizing: border-box;
        }
    </style>
</svelte:head>

<main class="container">
    <header class="header">
        <h1>🎉 Svelte 示例项目</h1>
        <p>展示Svelte 3.49.0的各种UI用法</p>
        <div class="time-display">当前时间: {currentTime}</div>
        <ToggleButton
            bind:checked={isDarkMode}
            on:click={() => app.toggleTheme()}
        >
            切换主题
        </ToggleButton>
    </header>

    <div class="content">
        <!-- 基础响应式数据展示 -->
        <section class="section">
            <h2>📊 响应式数据</h2>
            <div class="card">
                <p>你好，{name}！</p>
                <input
                    type="text"
                    on:input={onNameInput}
                    value={name}
                    placeholder="输入你的名字"
                />
                <p>计数器: {count}</p>
                <button on:click={() => app.incrementCount()}>增加</button>
                <button on:click={() => app.decrementCount()}>减少</button>
                <button on:click={() => app.resetCount()}>重置</button>
            </div>
        </section>

        <!-- 计算属性展示 -->
        <section class="section">
            <h2>📈 计算属性</h2>
            <div class="card">
                <div class="progress-container">
                    <div class="progress-bar">
                        <div
                            class="progress-fill"
                            style="width: {progress}%"
                        ></div>
                    </div>
                    <p>
                        任务进度: {completedTodos}/{totalTodos} ({progress.toFixed(
                            1,
                        )}%)
                    </p>
                </div>
            </div>
        </section>

        <!-- 组件展示 -->
        <section class="section">
            <h2>🧩 组件示例</h2>
            <div class="components-grid">
                <Counter />
                <UserCard name="张三" age={25} email="zhangsan@example.com" />
            </div>
        </section>

        <!-- 待办事项列表 -->
        <section class="section">
            <h2>📝 待办事项</h2>
            <TodoList />
        </section>

        <!-- 条件渲染 -->
        <section class="section">
            <h2>🔀 条件渲染</h2>
            <div class="card">
                {#if count > 10}
                    <p class="success">🎉 计数器已经超过10了！</p>
                {:else if count < 0}
                    <p class="warning">⚠️ 计数器是负数了！</p>
                {:else}
                    <p class="info">📊 计数器在正常范围内</p>
                {/if}
            </div>
        </section>

        <!-- 列表渲染 -->
        <section class="section">
            <h2>📋 列表渲染</h2>
            <div class="card">
                <h3>任务列表:</h3>
                <ul class="task-list">
                    {#each todos as todo (todo.id)}
                        <li
                            class="task-item {todo.completed
                                ? 'completed'
                                : ''}"
                        >
                            <input
                                type="checkbox"
                                checked={todo.completed}
                                on:change={() => app.toggleTodo(todo.id)}
                            />
                            <span>{todo.text}</span>
                            <button
                                class="remove-btn"
                                on:click={() => app.removeTodo(todo.id)}
                            >
                                删除
                            </button>
                        </li>
                    {/each}
                </ul>
            </div>
        </section>
    </div>
</main>

<style>
    .container {
        max-width: 1200px;
        margin: 0 auto;
        padding: 20px;
    }

    .header {
        text-align: center;
        margin-bottom: 40px;
        padding: 30px;
        background: rgba(255, 255, 255, 0.95);
        border-radius: 15px;
        box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
        backdrop-filter: blur(10px);
    }

    .header h1 {
        margin: 0 0 10px 0;
        font-size: 2.5rem;
        color: #2c3e50;
        text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.1);
    }

    .header p {
        margin: 0 0 20px 0;
        font-size: 1.2rem;
        color: #34495e;
    }

    .time-display {
        font-size: 1.1rem;
        color: #2c3e50;
        margin-bottom: 20px;
        padding: 10px;
        background: rgba(52, 152, 219, 0.1);
        border-radius: 8px;
        border: 1px solid rgba(52, 152, 219, 0.2);
    }

    .content {
        display: grid;
        gap: 30px;
    }

    .section {
        background: rgba(255, 255, 255, 0.95);
        border-radius: 15px;
        padding: 25px;
        box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
        backdrop-filter: blur(10px);
    }

    .section h2 {
        margin: 0 0 20px 0;
        color: #2c3e50;
        font-size: 1.5rem;
        border-bottom: 2px solid #3498db;
        padding-bottom: 10px;
    }

    .card {
        background: rgba(255, 255, 255, 0.8);
        border-radius: 10px;
        padding: 20px;
        border: 1px solid rgba(52, 152, 219, 0.2);
        box-shadow: 0 4px 16px rgba(0, 0, 0, 0.05);
    }

    .components-grid {
        display: grid;
        grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
        gap: 20px;
    }

    .progress-container {
        text-align: center;
    }

    .progress-bar {
        width: 100%;
        height: 20px;
        background: #ecf0f1;
        border-radius: 10px;
        overflow: hidden;
        margin-bottom: 10px;
        border: 1px solid #bdc3c7;
    }

    .progress-fill {
        height: 100%;
        background: linear-gradient(90deg, #2ecc71, #27ae60);
        transition: width 0.3s ease;
    }

    .task-list {
        list-style: none;
        padding: 0;
        margin: 0;
    }

    .task-item {
        display: flex;
        align-items: center;
        padding: 12px;
        margin: 8px 0;
        background: rgba(255, 255, 255, 0.9);
        border-radius: 8px;
        transition: all 0.3s ease;
        border: 1px solid #ecf0f1;
    }

    .task-item:hover {
        background: rgba(52, 152, 219, 0.1);
        transform: translateX(5px);
    }

    .task-item.completed {
        opacity: 0.7;
        background: rgba(46, 204, 113, 0.1);
    }

    .task-item span {
        flex: 1;
        margin-left: 10px;
        color: #2c3e50;
    }

    .task-item.completed span {
        text-decoration: line-through;
        color: #7f8c8d;
    }

    .remove-btn {
        background: #e74c3c;
        color: white;
        border: none;
        padding: 6px 12px;
        border-radius: 5px;
        cursor: pointer;
        transition: background 0.3s ease;
        font-size: 0.9rem;
    }

    .remove-btn:hover {
        background: #c0392b;
    }

    .success {
        color: #27ae60;
        font-weight: bold;
        padding: 10px;
        background: rgba(46, 204, 113, 0.1);
        border-radius: 5px;
        border-left: 4px solid #2ecc71;
    }

    .warning {
        color: #f39c12;
        font-weight: bold;
        padding: 10px;
        background: rgba(243, 156, 18, 0.1);
        border-radius: 5px;
        border-left: 4px solid #f39c12;
    }

    .info {
        color: #3498db;
        font-weight: bold;
        padding: 10px;
        background: rgba(52, 152, 219, 0.1);
        border-radius: 5px;
        border-left: 4px solid #3498db;
    }

    input[type="text"] {
        width: 100%;
        padding: 12px;
        border: 2px solid #ecf0f1;
        border-radius: 8px;
        background: white;
        color: #2c3e50;
        margin: 10px 0;
        font-size: 1rem;
        transition: border-color 0.3s ease;
    }

    input[type="text"]:focus {
        outline: none;
        border-color: #3498db;
        box-shadow: 0 0 0 3px rgba(52, 152, 219, 0.1);
    }

    input[type="text"]::placeholder {
        color: #95a5a6;
    }

    button {
        background: linear-gradient(45deg, #3498db, #2980b9);
        color: white;
        border: none;
        padding: 12px 24px;
        border-radius: 8px;
        cursor: pointer;
        margin: 5px;
        font-size: 1rem;
        font-weight: 500;
        transition: all 0.3s ease;
        box-shadow: 0 4px 12px rgba(52, 152, 219, 0.3);
    }

    button:hover {
        transform: translateY(-2px);
        box-shadow: 0 6px 20px rgba(52, 152, 219, 0.4);
    }

    button:active {
        transform: translateY(0);
    }

    /* 暗色主题样式 */
    :global(body.dark-mode) .header {
        background: rgba(44, 62, 80, 0.95);
        color: #ecf0f1;
    }

    :global(body.dark-mode) .header h1 {
        color: #ecf0f1;
    }

    :global(body.dark-mode) .header p {
        color: #bdc3c7;
    }

    :global(body.dark-mode) .time-display {
        color: #ecf0f1;
        background: rgba(52, 152, 219, 0.2);
    }

    :global(body.dark-mode) .section {
        background: rgba(44, 62, 80, 0.95);
        color: #ecf0f1;
    }

    :global(body.dark-mode) .section h2 {
        color: #ecf0f1;
        border-bottom-color: #3498db;
    }

    :global(body.dark-mode) .card {
        background: rgba(52, 73, 94, 0.8);
        border-color: rgba(52, 152, 219, 0.3);
    }

    :global(body.dark-mode) .task-item {
        background: rgba(52, 73, 94, 0.9);
        border-color: #34495e;
    }

    :global(body.dark-mode) .task-item span {
        color: #ecf0f1;
    }

    :global(body.dark-mode) .task-item.completed span {
        color: #95a5a6;
    }

    :global(body.dark-mode) input[type="text"] {
        background: #34495e;
        border-color: #2c3e50;
        color: #ecf0f1;
    }

    :global(body.dark-mode) input[type="text"]:focus {
        border-color: #3498db;
    }

    :global(body.dark-mode) input[type="text"]::placeholder {
        color: #7f8c8d;
    }

    /* 响应式设计 */
    @media (max-width: 768px) {
        .container {
            padding: 10px;
        }

        .header h1 {
            font-size: 2rem;
        }

        .components-grid {
            grid-template-columns: 1fr;
        }

        .section {
            padding: 20px;
        }
    }
</style>
